﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using GalaSoft.MvvmLight.Messaging;
using CommonLib;
using HalconDotNet;
namespace Project1.View
{
    /// <summary>
    /// UserControlHalConWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UserControlHalConWindow : UserControl
    {
        public bool ifRegisterMode = false;
        private ContextMenu cm ;
        private  double _scaleValue = 1;
        public UserControlHalConWindow()
        {
            InitializeComponent();
            cm = this.FindResource("cmButton") as ContextMenu;
            //int X = 0;
            //int Y = 0;

            //rect = new Rectangle()
            //{
            //    Margin = new Thickness(X, Y, 0, 0),
            //    Width = 100,
            //    Height = 200,
            //    Fill = Brushes.LightGoldenrodYellow,
            //    Stroke = Brushes.Black,
            //    StrokeThickness = 1,
            //    //DataContext = this.Key

            //};

            //rect.MouseLeftButtonDown += new MouseButtonEventHandler(test3_MouseLeftButtonnDown);
            //grid.Children.Add(rect);

        }
        private void test3_MouseLeftButtonnDown(object sender, MouseButtonEventArgs e)
        {
            double width = hWindowControlWPF1.ActualWidth;
            double height = hWindowControlWPF1.ActualHeight;
      
           // rect.Opacity = rect.Opacity + 1;
           // rect.Opacity = (int)rect.Opacity % 2;
        }

        private void Button_Back(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<string>("Back", "Reqback");
        }

        private void hSmartWindowControlWPF1_MouseRightButtonDown(object sender, EventArgs e)
        {
            cm = this.FindResource("cmButton") as ContextMenu;
            cm.PlacementTarget = sender as Button;
            cm.IsOpen = true;
        }

        private void OnRectangle1_Click(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<string>("rectangle1", "Draw");
        }

        private void OnRectangle2_Click(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<string>("rectangle2", "Draw");
        }

        private void OnCircle_Click(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<string>("circle", "Draw");
        }

        private void OnEllipse_Click(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<string>("ellipse", "Draw");
        }

        private void OnLine_Click(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<string>("line", "Draw");
        }
        private void OnClearAllObjects_Click(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<string>("clear", "Draw");
        }



        private void hSmartWindowControlWPF1_HInitWindow(object sender, EventArgs e)
        {
            //点击后能获得当前的位置
            
           // Messenger.Default.Send<HSmartWindowControlWPF>(hSmartWindowControlWPF1, "BindWindow");
        }

        private void hWindowControlWPF1_HInitWindow(object sender, EventArgs e)
        {
            Messenger.Default.Send<HWindowControlWPF>(hWindowControlWPF1, "BindWindow");
        }

    

        //鼠标移动的触发事件
        private void hWindowControlWPF1_HMouseMove(object sender, HMouseEventArgsWPF e)
        {
            //Messenger.Default.Send<int>(ConstValue.MouseMove, ConstValue.MOUSEEVENT);
        }

        //鼠标按键弹起的触发事件
        private void hWindowControlWPF1_HMouseUp(object sender, HMouseEventArgsWPF e)
        {
            Messenger.Default.Send<int>(ConstValue.MouseUp, ConstValue.MOUSEEVENT);
        }

        //鼠标滚轮的触发事件
        private void hWindowControlWPF1_HMouseWheel(object sender, HMouseEventArgsWPF e)
        {
           
            if (e.Delta == 0) return;
            double d = e.Delta / Math.Abs(e.Delta);
            if (_scaleValue < 0.5 && d < 0) return;
            if (_scaleValue > 5 && d > 0) return;
            double targetScaleValue;
            _scaleValue += d * 0.2;
            if (d > 0)
            {
                targetScaleValue = 1 + 0.2;
            }
            else
            {
                targetScaleValue = 1 - 0.2;
            }
            Messenger.Default.Send<double>(targetScaleValue, "Zoom");
        }

        private void hWindowControlWPF1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Messenger.Default.Send<int>(ConstValue.Adapt, ConstValue.MOUSEEVENT);
        }


        private void hWindowControlWPF1_HMouseDown(object sender, HMouseEventArgsWPF e)
        {
            currentSender = sender;

            if (e.Button == MouseButton.Right)
            {
                    Messenger.Default.Send<string>("clear", "Draw");
            }
            else if (e.Button == MouseButton.Left)
            {
                if(ifRegisterMode == true)
                {
                    Messenger.Default.Send<int>(ConstValue.MouseDown2, ConstValue.MOUSEEVENT);
                }
                else
                {
                    Messenger.Default.Send<int>(ConstValue.MouseDown, ConstValue.MOUSEEVENT);
                }
                 
            }

        }
        object currentSender = null;
        public  void OpenMenu()
        {  
            cm = this.FindResource("cmButton") as ContextMenu;
            cm.PlacementTarget = currentSender as Button;
            cm.IsOpen = true;
        }

        private void hWindowControlWPF1_HInitWindow_1(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 鼠标移动事件，用于拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hWindowControlWPF1_HMouseMove_1(object sender, HMouseEventArgsWPF e)
        {
            Messenger.Default.Send<int>(ConstValue.MouseMove, ConstValue.MOUSEEVENT);
        }
        /// <summary>
        /// 鼠标按键弹起标志着移动结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hWindowControlWPF1_HMouseUp_1(object sender, HMouseEventArgsWPF e)
        {
            //pressed = false;
            Messenger.Default.Send<int>(ConstValue.MouseUp, ConstValue.MOUSEEVENT);
        }
    }
}
